package com.lsq.lwf.common.xmlparse;

import java.io.File;
import java.lang.reflect.Method;
import java.util.Iterator;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Element;

import com.lsq.lwf.common.xmlparse.base.BaseXml;

public class ViewXmlParse extends BaseXml {

	public ViewXmlParse(File file) {
		super(file);

		Element rootElement = this.doc.getRootElement();

		try {

			viewParse(rootElement);

			String namespace = rootElement.attribute("name").getText().trim();

			viewNamespaceMap.put(namespace, viewMap);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void viewParse(Element rootElement) throws Exception {
		Element childElement = rootElement.element("view");

		Iterator<Element> childIter = childElement.elementIterator();

		String name = childElement.attribute("name").getText().trim();

		String cla = childElement.attribute("class").getText().trim();

		String method = childElement.attribute("method").getText().trim();

		if (StringUtils.isBlank(method)) {
			method = name;
		}

		Class c = Class.forName(cla);

		Method meth = c.getDeclaredMethod(method);
		viewMap.put(name+"-"+cla, meth);

		while (childIter.hasNext()) {

			Element e = childIter.next();

			String rname = e.attribute("name").getText().trim();
			String rpath = e.getTextTrim();

			resultMap.put(rname, rpath);
		}
	}

}
